<template>
    <div>
        <el-table :data="tableData"
                  :border="propJson.isBorder"
                :stripe="propJson.isStripe"
                :show-overflow-tooltip="propJson.isOverFlowTip"
                :max-height="propJson.isMaxHeight ? propJson.maxHeight : -1"
                :style="propJson.editFlag ? 'width: 98%; margin-left: 1%;' : ''"
                @selection-change="tableSelect"
                :row-key="propJson.rowKey"
                :default-expand-all="propJson.isShowAllChildren"
                :tree-props="{ children: propJson.childrenName, hasChildren: 'hasChildren' }"
        >
            <el-table-column v-if="propJson.isSelection" type="selection" width="45" />
            <el-table-column v-for="item in propJson.tableFieldList"
                             :prop="item.variableField"
                             :label="item.variableLabel"
                             :align="item.isAlignCenter">
                <template #default="scope" v-if="item.showType === 'custom'">
                    <slot :name="item.variableLabelEN" :scope="scope"></slot>
                </template>
            </el-table-column>

            <el-table-column align="center" :width="propJson.operationWidth ? propJson.operationWidth : 300" v-if="propJson.isAddOperate">
                <template #header>
                    <span>操 作</span>
                </template>
                <template #default="scope">
                    <slot :name="propJson.operateName" :scope="scope"></slot>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import {commonParamChange} from "@/utils/commonParamUtil";
import request from "@/utils/request";

export default {
    name: "TableComponent",
    props: ['propJson', 'commonParam'],
    data() {
        return {
            itemFlag: false,
            tableDataInfo: []
        }
    },
    methods: {
        tableSelect(val) {
            // 获取变量名称
            let paramName = this.propJson.selectVarParamName
            commonParamChange(this.commonParam, paramName, val)
        }
    },
    computed: {
        tableData() {
            if (this.propJson.editFlag) {
                return [];
            }
            // 判断参数类型是自定义的还是关联参数
            if (this.propJson.tableDataType === "custom") {
                if (Array.isArray(this.propJson.tableDataCustomInfo)) {
                    return this.propJson.tableDataCustomInfo
                } else {
                    let newStr = this.propJson.tableDataCustomInfo.replaceAll("\'", "\"");
                    return JSON.parse(newStr)
                }
            } else if (this.propJson.tableDataType === "variable") {
                // 循环遍历出对象
                for (let i = 0; i < this.commonParam.length; i++) {
                    let commonInfo = this.commonParam[i]
                    if (commonInfo.variableName === this.propJson.tableDataVariable) {
                        let commonVariableValue = commonInfo.variableValue
                        if (commonVariableValue === "" || commonVariableValue === undefined) {
                            return [];
                        }
                        if (Array.isArray(commonVariableValue)) {
                            return commonVariableValue
                        } else {
                            return JSON.parse(commonVariableValue)
                        }
                    }
                }
                return [];
            }
        }
    }
}
</script>

<style scoped lang="less">

</style>